package com.example.demo.web;

import com.example.demo.config.response.ResponseResult;
import com.example.demo.dao.dto.UserDTO;
import com.example.demo.service.UserDb1Service;
import com.example.demo.service.UserDb2Service;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * 数据源测试 controller
 */
@Api(tags = "数据源测试")
@RestController
@RequestMapping("/datasource")
public class DataSourceController {

    @Autowired
    private UserDb1Service userDb1Service;
    @Autowired
    private UserDb2Service userDb2Service;

    @GetMapping("/db1/{id}")
    @ApiOperation(value = "数据源1测试")
    public UserDTO getDb1UserInfo(@PathVariable("id") Integer id){
        return userDb1Service.queryUserById(id);
    }

    @GetMapping("/db2/{id}")
    @ApiOperation(value = "数据源2测试")
    public UserDTO getDb2UserInfo(@PathVariable("id") Integer id){
        return userDb2Service.queryUserById(id);
    }

    @PostMapping("/master-slave/{id}/{userName}")
    @ApiOperation(value = "Mysql主从测试. 主服务器新增数据")
    public void insertRecordToDb1(@PathVariable("id") Integer id, @PathVariable("userName") String userName){
        userDb1Service.insert(id, userName);
    }

    @GetMapping("/master-slave/{id}")
    @ApiOperation(value = "Mysql主从测试. 从服务器读取数据")
    public UserDTO getSlaveUserInfo(@PathVariable("id") Integer id){
        return userDb2Service.queryUserById(id);
    }

    @GetMapping("/pageHelperTest")
    @ApiOperation(value = "分页测试")
    public PageInfo<UserDTO> pageHelperTest(){
        return userDb1Service.queryUserByPage();
    }

}